package com.grpc.client.service;

import org.example.grpc.lib.HelloReply;
import org.example.grpc.lib.HelloRequest;
import org.example.grpc.lib.SimpleGrpc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import net.devh.boot.grpc.client.inject.GrpcClient;

@Service
public class GrpcClientService {
	
    @GrpcClient("biz-grpc-server")
    private Channel serverChannel;
    /*@Autowired
    private ManagedChannel serverChannel;*/

    public String sendMessage(String name) {
        SimpleGrpc.SimpleBlockingStub stub = SimpleGrpc.newBlockingStub(serverChannel);
        HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
        return response.getMessage();
    }
    
    public static void main(String[] args) {
    	String name = "test";
    	ManagedChannel serverChannel = ManagedChannelBuilder.forAddress("127.0.0.1", 8230)
                .usePlaintext()
                .build();
    	SimpleGrpc.SimpleBlockingStub stub = SimpleGrpc.newBlockingStub(serverChannel);
        HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
        String result = response.getMessage();
        
        System.out.println("result:" + result);
	}
}